/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Forum.java
 *
 * Created on 10/04/2011, 19:55:55
 */

package presentation.gui;

import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.SwingWorker;
import javax.swing.JPanel;

import presentation.gui.workers.SetFriendsWorker;

import common.forum.items.UserInfo;

import domain.ClientController;
import javax.swing.JOptionPane;
/**
 *
 * @author
 */
public class Forum extends javax.swing.JFrame {

    private static final long serialVersionUID = -2618735121811057973L;
    private javax.swing.JPanel _mainPanel;
    private StartWindow _start;

    ClientController controller;
    public static  DefaultListModel friendsList = new DefaultListModel();
    
    /** Creates new form Forum with main panel(forun/threads/posts) */
    public Forum(ClientController clientController, StartWindow start ) {
        controller = clientController;
        _start=start;
        setFriends();
        dispalyInitialForum( new ForumsViewPanel(controller, getStartWindow() ));
        initComponents();
        this.setSize(985,605);
        setVisible(true);
    }

    public void setMainPanel(JPanel panel){
        _mainPanel=panel;
         panel.setBounds(150, 0, 520, 470);
         this.setContentPane(panel);
         this.pack();
    }

   public void clearFriends(){
       friendsList.removeAllElements();
    }

    public void setFriends(){
    	
    	new SetFriendsWorker<Void, Void>(controller, this, friendsList).execute();

//        for (int i=0; i<controller.getFriendList().size(); i++  ){
//                 UserInfo friend = controller.getFriendList().get(i);
//                 friendsList.addElement(friend.getUserName());
//        }
    }

    public static DefaultListModel getFriendsListModel() {
        return friendsList;
    }

    public  JPanel getMainPanel(){
        return _mainPanel;
    }

    private StartWindow getStartWindow(){
        return _start;
    }

    public void displayForum(JPanel panel){
        if (panel!=null){
               this.remove(getMainPanel());
               setMainPanel(panel);
               initComponents();
               this.setSize(985,605);
               setVisible(true);
        }
        else
                  setVisible(false);
    }

    public void dispalyInitialForum(JPanel panel){
        if (panel!=null){
               setMainPanel(panel);
               initComponents();
               this.setSize(985,605);
               setVisible(true);
        }
        else
                  setVisible(false);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jButton1 = new javax.swing.JButton();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Hello User !", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Kristen ITC", 1, 14), new java.awt.Color(255, 255, 255))); // NOI18N

        jButton1.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/gui/pics/logout-icon.png"))); // NOI18N
        jButton1.setText("Logout");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(40, 500, 130, 30);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "My Friends", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Kristen ITC", 1, 14), new java.awt.Color(255, 255, 255))); // NOI18N

        jList1.setFont(new java.awt.Font("Kristen ITC", 1, 14)); // NOI18N
        jList1.setModel(presentation.gui.Forum.getFriendsListModel());
        jScrollPane1.setViewportView(jList1);

        jScrollPane1.setBounds(20, 40, 120, 170);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBounds(20, 40, 160, 230);
        jLayeredPane1.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/gui/pics/Edit-Users-icon.png"))); // NOI18N
        jButton2.setText("Edit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(40, 290, 110, 30);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton4.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/gui/pics/key.png"))); // NOI18N
        jButton4.setText("Change password");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton4.setBounds(10, 340, 180, 30);
        jLayeredPane1.add(jButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jLayeredPane1);
        jLayeredPane1.setBounds(740, 10, 200, 550);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/gui/pics/wallpaper_by_J_Sheldon.jpg"))); // NOI18N
        jLabel1.setMaximumSize(new java.awt.Dimension(970, 710));
        jLabel1.setMinimumSize(new java.awt.Dimension(970, 710));
        jLabel1.setPreferredSize(new java.awt.Dimension(970, 710));
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, -80, 960, 710);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        controller.logout();
        new StartWindow(controller).setSize(985,605);
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        new ManageFriends(controller, getStartWindow()).setSize(985,605);
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
       /*     if (jTextField1.getText().equals("")){
           // JOptionPane.showMessageDialog(null, "Please insert all details!", "Login Error", 0);
                System.out.println("here ");
       }
       else{
           String newPassword = jTextField1.getText();
           controller.changePass(newPassword);
System.out.println("password = " + newPassword);
       }*/
        new changePassword(_start, true,controller).setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
